//-----------------------------------------------------------------------
// <copyright file="CompanyAndTypeComDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CompanyAndTypeComDAOBase
    {
        #region Common methods
        public virtual CompanyAndTypeCom CreateCompanyAndTypeComFromReader(IDataReader reader)
        {
            CompanyAndTypeCom item = new CompanyAndTypeCom();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyAndTypeComID"))) item.CompanyAndTypeComID = (int)reader["CompanyAndTypeComID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyID"))) item.CompanyID = (long)reader["CompanyID"];
                if (!reader.IsDBNull(reader.GetOrdinal("TypeComID"))) item.TypeComID = (int)reader["TypeComID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndTypeComFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCompanyAndTypeCom methods
            
        public virtual void CreateCompanyAndTypeCom(CompanyAndTypeCom companyAndTypeCom)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndTypeComInsert");
                
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndTypeCom.CompanyID);
                database.AddInParameter(dbCommand, "@TypeComID", DbType.Int32, companyAndTypeCom.TypeComID);
                database.AddOutParameter(dbCommand, "@CompanyAndTypeComID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                companyAndTypeCom.CompanyAndTypeComID = (int)database.GetParameterValue(dbCommand, "@CompanyAndTypeComID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndTypeComException, ex);
            }
        }

        #endregion

        #region UpdateCompanyAndTypeCom methods
        
        public virtual void UpdateCompanyAndTypeCom(CompanyAndTypeCom companyAndTypeCom)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndTypeComUpdate");            
                
                database.AddInParameter(dbCommand, "@CompanyAndTypeComID", DbType.Int32, companyAndTypeCom.CompanyAndTypeComID);
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndTypeCom.CompanyID);
                database.AddInParameter(dbCommand, "@TypeComID", DbType.Int32, companyAndTypeCom.TypeComID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCompanyAndTypeComException, ex);
            }
        }
        
        #endregion

        #region DeleteCompanyAndTypeCom methods
        public virtual void DeleteCompanyAndTypeCom(int companyAndTypeComID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndTypeComDelete");
                
                database.AddInParameter(dbCommand, "@CompanyAndTypeComID", DbType.Int32, companyAndTypeComID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCompanyAndTypeComException, ex);
            }
        }

        #endregion

        #region GetCompanyAndTypeCom methods
        
        public virtual CompanyAndTypeCom GetCompanyAndTypeCom(int companyAndTypeComID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndTypeComGet");
                
                database.AddInParameter(dbCommand, "@CompanyAndTypeComID", DbType.Int32, companyAndTypeComID);
                
                CompanyAndTypeCom companyAndTypeCom =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        companyAndTypeCom = CreateCompanyAndTypeComFromReader(reader);
                        reader.Close();
                    }
                }
                return companyAndTypeCom;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndTypeComException, ex);
            }
        }
        
        #endregion

        #region GetCompanyAndTypeComList methods
        public virtual CompanyAndTypeComCollection GetCompanyAndTypeComList(CompanyAndTypeComColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndTypeComGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndTypeComCollection companyAndTypeComCollection = new CompanyAndTypeComCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndTypeCom companyAndTypeCom = CreateCompanyAndTypeComFromReader(reader);
                        companyAndTypeComCollection.Add(companyAndTypeCom);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndTypeComCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndTypeComListException, ex);
            }
        }
        
        public virtual CompanyAndTypeComCollection GetCompanyAndTypeComList(CompanyAndTypeComColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCompanyAndTypeComList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCompanyAndTypeComList methods
        public virtual CompanyAndTypeComCollection GetCompanyAndTypeComListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndTypeComGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndTypeComCollection companyAndTypeComCollection = new CompanyAndTypeComCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndTypeCom companyAndTypeCom = CreateCompanyAndTypeComFromReader(reader);
                        companyAndTypeComCollection.Add(companyAndTypeCom);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndTypeComCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndTypeComListException, ex);
            }
        }
        
        public virtual CompanyAndTypeComCollection GetCompanyAndTypeComListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCompanyAndTypeComListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}